package likou;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/07/09 20:56
 */
public class _43 {
    public static void main(String[] args) {
        System.out.println(multiply("999","0"));
    }
    static List<String> list = new ArrayList<>();

    static public String multiply(String num1, String num2) {
        if (num1.length() < num2.length()) {
            String temp = num1;
            num1 = num2;
            num2 = temp;
        }
        char[] cs1 = num1.toCharArray();
        char[] cs2 = num2.toCharArray();
        for (int i=cs2.length-1;i>=0;i--) {
            bfs(cs1, cs2[i]);
        }
        int wei = 1;//当前的位数，加上集合中的当前。
        String nowStr = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            char[] chars1 = nowStr.toCharArray();
            char[] chars2 = list.get(i).toCharArray();
            int cs1i = 0;
            int cs2i = 0;
            if (chars2.length != 0) {
                StringBuilder nowSb = new StringBuilder();
                while (cs1i < wei) {
                    nowSb.append(chars1[cs1i]);
                    cs1i++;
                }
                int pre = 0;
                while (cs1i < chars1.length && cs2i < chars2.length) {
                    int now = pre + (chars1[cs1i]-'0') + (chars2[cs2i]-'0');
                    pre = now / 10;
                    now %= 10;
                    nowSb.append(now);
                    cs1i++;
                    cs2i++;
                }
                while (cs2i != chars2.length) {
                    int now = (chars2[cs2i]-'0') + pre;
                    pre = now / 10;
                    now %= 10;
                    nowSb.append(now);
                    cs2i++;
                }
                if (pre!=0){
                    nowSb.append(pre);
                }
                nowStr = nowSb.toString();
            }
            wei++;
        }
        String res = new StringBuilder(nowStr).reverse().toString().replaceAll("^(0+)", "");
        return res.length()==0?"0":res;
    }

    static public void bfs(char[] cs1, char c2) {
        StringBuilder sb = new StringBuilder();
        int c2n = c2 - '0';
        int pre = 0;
        for (int i = cs1.length - 1; i >= 0; i--) {
            int now = pre + (cs1[i] - '0') * c2n;
            pre = now / 10;
            now %= 10;
            sb.append(now);
        }
        if (pre != 0) {
            sb.append(pre);
        }
        list.add(sb.toString());
    }
}
